package com.maoyan.quickdevelop.admin.controller.system;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.maoyan.quickdevelop.common.StringUtils;
import com.maoyan.quickdevelop.common.constant.HttpStatus;
import com.maoyan.quickdevelop.common.core.AjaxResult;
import com.maoyan.quickdevelop.common.core.domain.DqUser;
import com.maoyan.quickdevelop.system.domain.vo.LoginVO;
import com.maoyan.quickdevelop.system.service.IDqUserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 猫颜
 * @date 2021/5/28 11:21
 * 用户登陆
 * TODO 用户登陆控制类
 */
@RestController
public class DqLoginController {
    @Autowired
    private IDqUserService iUserService;

    @ApiOperation(value = "用户登陆")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginVO loginVO) {

        DqUser dqUser = iUserService.selectDqUserByUserNameAndPassword(loginVO.getUserName()
                , SaSecureUtil.md5(SaSecureUtil.sha1(loginVO.getPassWord())));
        //去掉用户的password
        dqUser.setPassWord("000000");
        /** 断言适合做测试开发，不适合实际生产 **/
        //Assert.notNull(dqUser,"用户名密码错误");
        //Sa-Token登陆
        //TODO 登陆并使用数据库用户ID来作为Satoken的用户ID
        StpUtil.setLoginId(dqUser.getUserId(), true);
//        StpUtil.setLoginId(dqUser.getUserName(),true);
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        System.out.println(tokenInfo.tokenName);
        System.out.println(tokenInfo.tokenValue);
        //传入tokeninfo,不再使用传统的cookie
//        		"tokenName": "MaoyanToken",
//		"tokenValue": "e4fd232e-c5cf-4f00-afa7-f3d4875ede6b
        return AjaxResult.success("登陆成功", tokenInfo);
    }


}
